package com.leancloud.circle.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.aries.ui.view.title.TitleBarView;
import com.leancloud.circle.R;
import com.leancloud.circle.adapter.FansAdapter;
import com.leancloud.circle.bean.FansBean;
import com.leancloud.circle.manage.CircleBack;
import com.leancloud.circle.manage.CircleGlobal;
import com.leancloud.circle.manage.CirclePathManage;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.utils.ToastUtil;

@Route(path = CirclePathManage.CIRCLE_FOLLOWERS)
public class MyFollowersActivity extends BaseTitleActivity {

    private SmartRefreshLayout smartRefreshLayout;
    private RecyclerView recyclerView;
    private FansAdapter fansAdapter;

    private List<FansBean> fansBeans;

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText("我的关注");
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_my_followers;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        smartRefreshLayout = findViewById(R.id.smart_refresh_layout);
        recyclerView = findViewById(R.id.my_attention_recycler);

        fansAdapter = new FansAdapter(R.layout.circle_layout_item_fans);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(fansAdapter);

        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                requestAttentionData();
            }
        });

        if (fansBeans == null || fansBeans.isEmpty()) {
            smartRefreshLayout.autoRefresh();
        }
    }

    private void requestAttentionData() {
        CircleGlobal.getCircleImp().requestConcerned(new CircleBack<List<FansBean>>() {
            @Override
            public void onBack(List<FansBean> beans, String err) {
                smartRefreshLayout.finishRefresh();
                if (err == null) {
                    fansBeans = beans;
                }
                setDataToView();
            }
        });
        CircleGlobal.getCircleImp().requestFans(null);
    }

    private void setDataToView() {
        if (fansBeans == null || fansBeans.isEmpty()) {
            ToastUtil.show("您还没有关注哦");
        } else {
            fansAdapter.replaceData(fansBeans);
            fansAdapter.notifyDataSetChanged();
        }
    }
}
